package com.topscomm.pm.script;

import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.pm.PmScript;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import com.topscomm.tap.common.DatacodeCache;

/**
 * @ClassName: ToaExpressApplyScript
 * @Description: 调用物流接口，生成快递发货单
 * @author lanjianwei
 * @date 2020年2月24日
 *
 */
public class PmExpressApplyScript extends PmScript {

	/**
	 * @Title: expressOrder
	 * @Description: 快递发货申请审批结束调用物流接口进行快递下单
	 * @param @param service
	 * @param @param entity  参数
	 * @return void    返回类型
	 * @throws Exception 
	 * @throws
	 * @author lanjianwei
	 */
	public void expressOrder(ITapService service, TapEntity entity) throws Exception {
		// 审批节点实例
		CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
		String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
		// 来源单据类型
		String sourcetype = nodeins.getSourcetype();
		DatacodeCache datacodeCache = (DatacodeCache) SpringUtil.getBean("datacodeCache");
		/*ToaExpressApplyService toaExpressApplyService = (ToaExpressApplyService) SpringUtil
				.getBean("toaExpressApplyService");
		ToaExpressApplyLineService toaExpressApplyLineService = (ToaExpressApplyLineService) SpringUtil
				.getBean("toaExpressApplyLineService");
		// 快递发货申请主表entity
		ToaExpressApplyEntity toaExpressApplyEntity = toaExpressApplyService.queryById(sourceid);
		// 快递发货申请子表entity
		StringBufferProxy whereSql = new StringBufferProxy();
		whereSql.appendLineSingle("expressapplyid='{0}'", sourceid);
		List<ToaExpressApplyLineEntity> expressApplyLineList = toaExpressApplyLineService
				.queryByWhere(whereSql.toString());
		//listLine存子表行map
		List<Map<String, Object>> listLine = new ArrayList<>();
		for (ToaExpressApplyLineEntity toaExpressApplyLineEntity : expressApplyLineList) {
			String itemname = toaExpressApplyLineEntity.getItemname();
			int num = toaExpressApplyLineEntity.getNum();
			String description = toaExpressApplyLineEntity.getDescription();
			Map<String, Object> map = new HashMap<>(4);
			map.put("itemname", itemname);
			map.put("num", num);
			map.put("description", description);
			listLine.add(map);
		}
		//paramEntity存接口所需字段
		Entity paramEntity = new Entity();
		paramEntity.set("sourcedocno", toaExpressApplyEntity.getDocno());
		paramEntity.set("sourcesys", "oa");
		paramEntity.set("sourcemodule", sourcetype);
		paramEntity.set("sourcemoduledoctype", sourcetype);
		paramEntity.set("companycode", toaExpressApplyEntity.getBusinesscompanycode());
		paramEntity.set("companyname", toaExpressApplyEntity.getBusinesscompanyname());
		paramEntity.set("deptcode", toaExpressApplyEntity.getDeptcode());
		paramEntity.set("deptname", datacodeCache.getValueDynamic("CboDeptFull", 
					toaExpressApplyEntity.getDeptcode(), "code","name"));
		paramEntity.set("usercode", toaExpressApplyEntity.getUsercode());
		paramEntity.set("username", toaExpressApplyEntity.getUsername());
		paramEntity.set("j_usercompany", toaExpressApplyEntity.getJ_usercompany());
		paramEntity.set("j_username", toaExpressApplyEntity.getJ_username());
		paramEntity.set("j_usertel", toaExpressApplyEntity.getJ_usertel());
		paramEntity.set("j_usermobile", toaExpressApplyEntity.getJ_usermobile());
		paramEntity.set("j_useraddress", toaExpressApplyEntity.getJ_useraddress());
		paramEntity.set("d_usercompany", toaExpressApplyEntity.getD_usercompany());
		paramEntity.set("d_username", toaExpressApplyEntity.getD_username());
		paramEntity.set("d_usertel", toaExpressApplyEntity.getD_usertel());
		paramEntity.set("d_usermobile", toaExpressApplyEntity.getD_usermobile());
		paramEntity.set("d_useraddress", toaExpressApplyEntity.getD_useraddress());
		paramEntity.set("express", toaExpressApplyEntity.getExpress());
		paramEntity.set("pay_method", toaExpressApplyEntity.getPay_method());
		paramEntity.set("listLine", listLine);*/
//		String params = JSON.toJSONString(paramEntity);
//		// 调用物流接口
//		ICboSysParamService cboSysParamService = (CboSysParamService)SpringUtil.getBean("cboSysParamService");
//		List<CboSysParamEntity> cboSysParamEntities = cboSysParamService.queryByWhere("code = 'ToaExpressOrder' and category = 'Toa'");
//        if(cboSysParamEntities.size()<=0) {     
//            throw new SchedulerConfigException("物流下单地址URL为空，请联系管理员维护！");
//        }
//		String url =cboSysParamEntities.get(0).getParamvalue();
//		String responseMsg = "";
//		try {
//			//接收返回信息
//			responseMsg = DpHttpPostUtil.postHttp(url, params);
//			JSONObject obj = JSONObject.parseObject(responseMsg);
//			if (!obj.getBoolean("success")) {
//				throw new ScriptException(obj.getString("msg"));
//			}else {
//				toaExpressApplyEntity.setExpressid(obj.getLongValue("data"));
//				toaExpressApplyService.updateEntity(toaExpressApplyEntity);
//			}
//			
//		} catch (Exception ex) {
//			throw new Exception(ex.getMessage());
//		}

	}
}
